package com.ghsc.ghxc.jsb.ui.live.view.cust

import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.cn.appcore.ext.QuickAdapter
import com.cn.appcore.ext.gone
import com.cn.appcore.ext.load
import com.cn.appcore.ext.setOnFastClickListener
import com.cn.appcore.ext.setVisible
import com.cn.appcore.ext.startKtxActivity
import com.cn.appcore.ext.transformRoundCorner
import com.cn.appcore.ext.visible
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.databinding.ImCustShareSourceViewBinding
import com.ghsc.ghxc.jsb.ui.live.bean.MaterialChildInfoBean
import com.ghsc.ghxc.jsb.ui.mall.activity.VideoPlayerActivity
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.makeramen.roundedimageview.RoundedImageView
import com.netease.nimlib.sdk.msg.constant.MsgDirectionEnum
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatBaseMessageViewHolderBinding
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean
import com.netease.yunxin.kit.chatkit.ui.normal.view.message.viewholder.NormalChatBaseMessageViewHolder
import com.zzhoujay.richtext.RichText

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.live.view.cust
 * 类名：CusShareSourceAttachmentViewHolder
 * 创建时间：2024/5/17 12:03
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class CusShareSourceAttachmentViewHolder (parent: ChatBaseMessageViewHolderBinding, viewType: Int) : NormalChatBaseMessageViewHolder(
    parent, viewType
) {

    private var binding //创建自己的layout布局
            : ImCustShareSourceViewBinding? = null

    override fun addViewToMessageContainer() {
        binding = ImCustShareSourceViewBinding.inflate(
            LayoutInflater.from(parent.context), messageContainer, true
        )
    }

    //去掉背景
    override fun onMessageBackgroundConfig(messageBean: ChatMessageBean?) {
        baseViewBinding.messageContainer.setBackgroundResource(R.color.title_transfer)
    }


    override fun bindData(message: ChatMessageBean?, lastMessage: ChatMessageBean?) {
        super.bindData(message, lastMessage)
        currentMessage = message
        val bean = message!!.messageData.message.attachment as CusShareSourceAttachment
            ?: return



        binding?.let {
            if(!TextUtils.isEmpty(bean.contentX)){
                it.tvDetail.visible()
//                val webSettings: WebSettings = it.webView.settings
//                webSettings.javaScriptEnabled = true //允许使用js
//                //设置WebView是否支持使用屏幕控件或手势进行缩放，默认是true，支持缩放
//                webSettings.setSupportZoom(false)
//                //设置WebView是否使用其内置的变焦机制，该机制集合屏幕缩放控件使用，默认是false，不使用内置变焦机制
//                webSettings.builtInZoomControls = false
//                //置WebView使用内置缩放机制时，是否展现在屏幕缩放控件上，默认true，展现在控件上
//                webSettings.displayZoomControls = false
////                webView.addJavascriptInterface(AndroidInterface(),"android")
//                it.webView.loadData(bean.contentX, "text/html", "UTF-8")
//                RichText.fromHtml( bean.contentX ).into(it.tvDetail)
                it.tvDetail.text = bean.contentX
            }else{
                it.tvDetail.gone()
            }
            val materialChildListAdapter = MaterialChildListAdapter()
            it.rvImg.adapter = materialChildListAdapter
            it.rvImg.layoutManager = GridLayoutManager(parent.context, 3)


            val images = bean.images
            if (!TextUtils.isEmpty(images)) {
                it.rvImg.visible()
                //images 是Json 转list
                var materialChildList = mutableListOf<MaterialChildInfoBean>()
                materialChildList = Gson().fromJson(
                    images,
                    object : TypeToken<List<MaterialChildInfoBean>>() {}.type
                )
                materialChildListAdapter.setNewInstance(materialChildList)
            } else {
                it.rvImg.gone()
            }
            materialChildListAdapter.setOnFastClickListener { view, position ->
//                if (materialChildListAdapter.getItem(position).type==1){
//                    parent.context.startKtxActivity<VideoPlayerActivity>(
//                        value =  "video_path" to materialChildListAdapter.getItem(position).url
//                    )
//                }else{
//                    RxBus.getDefault().post(RxBusBean(CustKey.IM_IMG_VIDEO,  CustKey.IM_IMG_VIDEO, position,  materialChildListAdapter.data))
//                }
                RxBus.getDefault().post(RxBusBean(CustKey.IM_IMG_VIDEO,  CustKey.IM_IMG_VIDEO, position,  materialChildListAdapter.data))
            }
            if (message!!.messageData.message.direct == MsgDirectionEnum.Out) {
                //右边，发送
                it.layoutLeft.gravity = Gravity.CENTER or Gravity.END
            } else {
                //左边  接受
                it.layoutLeft.gravity = Gravity.CENTER or Gravity.START
            }
        }
    }


    inner class MaterialChildListAdapter :
        QuickAdapter<MaterialChildInfoBean>(R.layout.share_item_img, convert = { helper, item ->
            val imageView = helper.getView<ImageView>(R.id.imageView)
            val ivPlayer = helper.getView<ImageView>(R.id.ivPlayer)
            val isVideo = helper.getView<RoundedImageView>(R.id.isVideo)
            ivPlayer.visibility = if (item.type == 1) View.VISIBLE else View.GONE
            isVideo.visibility = if (item.type == 1) View.VISIBLE else View.GONE
            imageView.load(item.url, builder = {
                transform(transformRoundCorner(4))
            })

        })
}
